#pragma once


class NAMEDPIPELOGWRITER_EXPORTS_INTERFACE CNamedPipeLogWriter
{
	static const BYTE PENDING_CONNECT_STATE = 0;
	static const BYTE READING_STATE = 1; 
	static const BYTE PENDING_WRITE_STATE = 2; 
	static const WORD PIPE_TIMEOUT = 5000;
	static const WORD BUFSIZE = 4096;	


	static UINT __stdcall doWork( LPVOID lpParam );

	
	struct PIPE_OVERLAPPED : public OVERLAPPED
	{
		TCHAR					chReply[BUFSIZE];
		DWORD					cbToWrite;
		CNamedPipeLogWriter*	pLogInstance;
	};

	HANDLE	m_hPipe;
	HANDLE	m_hEventToConnect;
	BOOL	m_isRunning;
	TCHAR	m_pipePath[MAX_PATH];
	
	static VOID WINAPI CompletedWriteRoutine(DWORD dwErr, DWORD cbWritten, LPOVERLAPPED lpOverLap);
	BOOL DisconnectAndReconnect(); 	

public:
	CNamedPipeLogWriter(const TCHAR* pPipePath);
	~CNamedPipeLogWriter();

	void AsyncLog(const TCHAR *format, ...);
	void Log(const TCHAR *format, ...);
	void AsyncVLog(const TCHAR *format, va_list arglist);
	void VLog(const TCHAR *format, va_list arglist);

};	
